//
//  DateTimeViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 9/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DateTimeViewController.h"

@implementation NSDictionary (Extension)

- (id)newObjectForKey:(id)aKey
{
    id rez = [self objectForKey:aKey];
    
    return ([rez isKindOfClass:[NSNull class]] ? nil : rez);
}
@end


@implementation DateTimeViewController

@synthesize datePickerView,startStr,endStr,myCell,data,dateStr,startTime,submitedDate;

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)done:(id)sender
{
    [self.data setObject:endStr forKey:@"endTime"];
    [self.data setObject:self.startTime forKey:@"startTime"];
    [self.data setObject:dateStr forKey:@"eventDate"];
    [self.data setObject:self.submitedDate forKey:@"submitedDate"];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)initTopBar
{
    [self setRightButtonImageActiveName:nil 
                           inactiveName:@"done-button" 
                                 target:self 
                                 action:@selector(done:)];
    
    [self setLeftButtonImageActiveName:nil 
                          inactiveName:@"back-button.png" 
                                target:self 
                                action:@selector(back:)];
    [self setTitle:@"Time of event"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg"]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTopBar];
    //[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"h:mm a"];
    self.startTime = [dateFormatter stringFromDate:self.datePickerView.date];
    
    [dateFormatter setDateFormat:@"dd MMMM"];
    self.dateStr = [dateFormatter stringFromDate:self.datePickerView.date];
    NSLog(@"dateStr = %@",self.dateStr);
    
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *theTime;
    theTime = [dateFormatter stringFromDate:[datePickerView date]];
    self.startStr = theTime; 
    NSLog(@"startStr = %@",self.startStr);
    self.endStr = @"";
    [dateFormatter release];
    
    NSDateFormatter* formatter;
    formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.submitedDate = [formatter stringFromDate:self.datePickerView.date];
    [formatter release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)dealloc
{
    [myCell release];
    [data release];
    [startStr release];
    [endStr release];
    [datePickerView release];
    [startTime release];
    [dateStr release];
    [submitedDate release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}



- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    static NSString *DateTimeCellCellIdentifier = @"DateTimeCell";
    cell = [tableView dequeueReusableCellWithIdentifier:DateTimeCellCellIdentifier];
    if (cell == nil) {
        cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:DateTimeCellCellIdentifier] autorelease];
        [[NSBundle mainBundle] loadNibNamed:@"DateTimeCell" owner:self options:nil];
        cell = self.myCell;
        self.myCell = nil;  
    } 
    UILabel *firstText = (UILabel *)[cell viewWithTag:1];
    UILabel *secondText = (UILabel *)[cell viewWithTag:2];
    firstText.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
    secondText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];

    switch (indexPath.row) {
        case 0: 
        {
            firstText.text = @"Starts:";
            secondText.text = startStr;
            

            break;
        }
        case 1: 
        {
            firstText.text = @"End:";  
            secondText.text = endStr;
            
            break;
        }
                default:
        {
            break;
        }
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
    }else
    {
        datePickerView.datePickerMode = UIDatePickerModeTime;
    }
    
}

#pragma mark -
#pragma mark UIPickerViewDelegate

-(IBAction) timeChange: (id)sender
{
	if (datePickerView.datePickerMode == UIDatePickerModeTime) {
        NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
        [outputFormatter setDateFormat:@"h:mm a"];
        
        self.endStr = [outputFormatter stringFromDate:self.datePickerView.date];
        
        [outputFormatter release];

    }else {
        
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        
        [dateFormatter setDateFormat:@"h:mm a"];
        self.startTime = [dateFormatter stringFromDate:self.datePickerView.date];
        NSLog(@"startTime = %@",self.startTime);
        
        [dateFormatter setDateFormat:@"dd MMMM"];
        self.dateStr = [dateFormatter stringFromDate:self.datePickerView.date];
        NSLog(@"dateStr = %@",self.dateStr);
        
        NSDateFormatter* formatter;
         formatter = [[NSDateFormatter alloc]init];
         [formatter setDateFormat:@"yyyy-MM-dd"];
         self.submitedDate = [formatter stringFromDate:self.datePickerView.date];
        [formatter release];
        NSLog(@"submitedDate = %@",self.submitedDate);

        
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        NSString *theTime;
        theTime = [dateFormatter stringFromDate:[datePickerView date]];
        self.startStr = theTime; 
        NSLog(@"startStr = %@",self.startStr);
        
        [dateFormatter release];
    }
    [self.tableView reloadData];
	
}


@end
